package com.fanqwz.kotlinmvvm.ui.activity.login

import android.arch.lifecycle.ViewModel
import android.arch.lifecycle.ViewModelProvider
import android.arch.lifecycle.ViewModelProviders
import com.fanqwz.kotlinmvvm.di.module.BaseActivityModule
import com.fanqwz.kotlinmvvm.di.scope.PerActivity
import com.fanqwz.kotlinmvvm.presenter.viewmodel.login.ILoginViewModel
import com.fanqwz.kotlinmvvm.presenter.viewmodel.login.LoginViewModel
import dagger.Binds
import dagger.Module
import dagger.Provides


/**
 * Created by fanqwz on 2018/3/2.
 */

/**
 * !!!必须使用abstact方法和Binds注解，或者使用Provides注解和静态方法，否则会运行出错*/
@Module
abstract class LoginActivityModule(activity: LoginActivity) : BaseActivityModule<LoginActivity>() {
//    @Binds
//    @PerActivity
//    abstract fun provideRepo(userDataRepository: UserDataRepository): UserRepository

//    @PerFragment
//    @ContributesAndroidInjector(modules = [MainFragmentModule::class])
//    internal abstract fun mainFragment(): MainFragment

    @Binds
    @PerActivity
    abstract fun provideView(loginview: LoginView): ILoginView

    //    @Binds
//    @PerActivity
//    abstract  fun provideViewModel(loginViewModel: LoginViewModel): ILoginViewModel
    @Module
    companion object {
        //        @JvmStatic
//        @Provides
//        @PerActivity
//        fun provideLoginView(): ILoginView {
//            return LoginView
//        }
//        @JvmStatic
//        @Provides
//        @PerActivity
//        fun providerFactory(loginViewModel: LoginViewModel): ViewModelProvider.Factory {
//            return object : ViewModelProvider.Factory {
//                override fun <T : ViewModel?> create(modelClass: Class<T>): T {
//                    if (modelClass.isAssignableFrom(LoginViewModel::class.java))
//                        return loginViewModel as T
//                    else
//                        throw IllegalArgumentException("class can't be inject:$modelClass")
//                }
//            }
//        }

        @JvmStatic
        @Provides
        @PerActivity
        fun provideLoginViewModel(activity: LoginActivity, factory:ViewModelProvider.Factory): ILoginViewModel {
            return ViewModelProviders.of(activity, factory).get(LoginViewModel::class.java)
        }
    }
}